package com.admin.config;

import com.admin.interfaces.intercept.NavMenuActiveInterceptor;
import com.admin.interfaces.intercept.OauthInterceptor;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.Arrays;
import java.util.List;

/**
 * @author Jonsy
 */
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
    @Autowired(required = false)
    private OauthInterceptor oauthInterceptor;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/to-login").setViewName("login");
        registry.addViewController("/error").setViewName("error");
        registry.addViewController("/403").setViewName("error/403");
        registry.addViewController("/401").setViewName("error/401");
        registry.addViewController("/404").setViewName("error/404");

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new NavMenuActiveInterceptor());
        registry.addInterceptor(oauthInterceptor).addPathPatterns("/h5/**", "/wechat/**","/images/**").excludePathPatterns("/wechat/paynotify");
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter4 jsonHttpMessageConverter = new FastJsonHttpMessageConverter4();
        jsonHttpMessageConverter.setSupportedMediaTypes(Arrays.asList(new MediaType("application", "json")));
        converters.add(jsonHttpMessageConverter);
        super.configureMessageConverters(converters);
    }
}
